<template>
  <div class="app-container">
    <ContentWrap>
      <el-row :gutter="20">
        <el-col :span="4">
          <RegionTree node-type="3" :node-click="handleDyhNodeClick"/>
        </el-col>

        <el-col :span="20" class="content-container">
          <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                   label-width="68px">
            <el-form-item label="报表类型" prop="frozenType">
              <el-radio-group v-model="queryParams.frozenType" @change="handleFrozenTypeChange">
                <el-radio label="day">日</el-radio>
                <el-radio label="month">月</el-radio>
                <!--              <el-radio label="year">年</el-radio>-->
              </el-radio-group>
            </el-form-item>
            <el-form-item label="开始日期" prop="startDate">
              <el-date-picker v-model="queryParams.startDate" :type="dateType" :value-format="valueType"
                              placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束日期" prop="endDate">
              <el-date-picker v-model="queryParams.endDate" :type="dateType" :value-format="valueType"
                              placeholder="选择日期"></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>

          <el-table v-loading="loading" :data="freezeDfqList" border :height="getAvailableHeight">
            <!--      <el-table-column type="selection" width="55" align="center"/>-->
            <!--      <el-table-column label="设备归属租户" align="center" prop="tenantId"/>-->
            <!--      <el-table-column label="层级id" align="center" prop="areaid"/>-->
            <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
            <el-table-column label="名称" align="center" prop="zone.areaName"/>
            <el-table-column label="日期" align="center" prop="freezetime"/>
            <el-table-column label="用水量(m³)" align="center" prop="flowDaily" sortable/>
            <el-table-column label="最小夜间流量(m³/h)" align="center" prop="mnf" sortable/>
            <el-table-column label="夜间流量(m³)" align="center" prop="nightFlow" sortable/>
            <!--      <el-table-column label="冻结类型，日：day，月：month" align="center" prop="frozenType"/>-->
            <el-table-column label="环比(%)" align="center" prop="hb" sortable>
              <template slot-scope="scope">
                <span v-if="!scope.row.hb || scope.row.hb==='-'">-</span>
                <span v-else :style="getHbStyle(scope.row.hb)">{{ scope.row.hb }}</span>
              </template>
            </el-table-column>
          </el-table>

          <pagination
            :total="total"
            :page-sizes="[15, 30, 45, 60]"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
    </ContentWrap>
  </div>
</template>

<script>
import {listByQueryModel} from "@/api/leakage/freezeDyh";
import RegionTree from "@/views/leakage/components/RegionTree.vue";

export default {
  name: "FreezeDyh",
  components: {RegionTree},
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 大用户冻结数据表格数据
      freezeDfqList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        areaId: null,
        frozenType: 'day',
        startDate: '',
        endDate: '',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dateType: 'date',
      valueType: 'yyyy-MM-dd',
      editingZone: undefined
    };
  },
  created() {
    // this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 235
    },
  },
  methods: {
    /** 查询大用户冻结数据列表 */
    getList() {
      this.loading = true;
      if (this.editingZone) {
        this.queryParams.areaId = this.editingZone.areaId
      }
      listByQueryModel(this.queryParams).then(response => {
        this.freezeDfqList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('swla2/freezeDyh/export', {
        ...this.queryParams
      }, `大用户绩效报表.xlsx`)
    },
    handleFrozenTypeChange(val) {
      if (val === 'day') {
        this.dateType = 'date'
        this.valueType = 'yyyy-MM-dd'
      } else if (val === 'month') {
        this.dateType = 'month'
        this.valueType = 'yyyy-MM'
      } else {
        this.dateType = 'year'
        this.valueType = 'yyyy'
      }
    },
    handleDyhNodeClick(node) {
      const levelType = node.levelType
      if (levelType !== '3') {
        this.editingZone = undefined
        this.$message.error('请选择大用户节点')
      } else {
        this.editingZone = node
        this.handleQuery()
      }
    },
    getHbStyle(hb) {
      let color
      const fHb = parseFloat(hb)
      if (fHb > 0) {
        color = 'red'
      } else {
        color = 'green'
      }
      return {color: color}
    },
  }
};
</script>
